const request = require('../../../../../utils/request');
const {computed,watch} = require('../../../../../utils/computed')

Page({
    data: {
        list:[0,1,2,3,4],
        roomTable: [],
        roomTables: [],
        currentDesk: [],
        arr0: [],   //房间数组
        arr1: [],
        consume0: [],   //最低消费数组
        consume1: [],
        selectRoom0: [], //选中的房桌
        selectRoom1: [],
        arrive_time: '',
        shop_id: '',
        address_name: '',
        address_tel: '',
        room:{},    //房间详情数据
        seeDetail: false,    //详情是否可看
        imgUrl: request.imgUrl,
        indicatorDots: true,
        autoplay: true,
        circular: false,
        interval: 4000,
        duration: 500,
    },
    onLoad: function(e) {
        this.setData({
            arrive_time: e.arrive_time,
            shop_id: e.shop_id
        })
        request.HttpRequst(true,'checkDeskRoom',2,'',{"arrive_time":e.arrive_time,"shop_id":e.shop_id},"GET",false,res => {
            console.log(res);
            if( res.code == 200 ){
                this.setData({
                    roomTable: res.data
                });
                let a = [],b=[];
                let arr = [];
                this.data.roomTable.forEach(res => {
                    if(res.type == 0){
                        a.push(res);
                    }else{
                        b.push(res)
                    }
                });
                arr.push(a);
                arr.push(b);
                this.setData({
                    roomTables: arr
                })
            }else{
                wx.showModal({
                    title: '提示',
                    content: 'res.info',
                    showCancel: false
                })
            }
        })

        request.HttpRequst(true, 'user/allAddress', false, '', { "token": wx.getStorageSync("sessionId")}, "post", false,res =>{
            if(res.code==200){
                res.data.forEach( val => {
                    if(val.pretermission == 1){
                        this.setData({
                            address_name:val.name,
                            address_tel:val.mobile,
                        })
                    }
                })
            }else{
              return
            }
        })
        
    },
    onReady: function(){
        wx.setNavigationBarTitle({
            title: '选择房桌'
        })   
    },
    checkboxChange0: function(e) {
        console.log('checkbox发生change事件，携带value值为：', e.detail.value);
        let a=[],b=[];
        e.detail.value.forEach( res => {
            a.push(res.split(',')[0]);
            b.push(res.split(',')[1]);
        })
        console.log(a)
        this.setData({
            arr0: a,
            consume0: b,
            selectRoom0: e.detail.value
        })
        // let val = e.detail.value;
        // let unique = [...new Set(this.data.arr.concat(val))];
        // this.setData({
        //     arr: unique
        // })
        // console.log(unique.join("|"))
    },
    checkboxChange1: function(e) {
        console.log('checkbox发生change事件，携带value值为：', e.detail.value);
        let a=[],b=[];
        e.detail.value.forEach( res => {
            a.push(res.split(',')[0]);
            b.push(res.split(',')[1]);
        })
        console.log(a)
        this.setData({
            arr1: a,
            consume1: b,
            selectRoom1: e.detail.value
        })
    },
    getName(e) {
        this.setData({
            address_name: e.detail.value
        })
    },
    getTel(e) {
        this.setData({
            address_tel: e.detail.value
        })
    },
    goAdvList() {
        if(this.data.address_name == '' || this.data.address_tel == ''){
            wx.showModal({
                title: '提示',
                content: '请填写姓名和电话！',
                showCancel: false
            })
            return false;
        }
        let desk_room = [].concat(this.data.arr0,this.data.arr1).join("|");
        let min_consumptionArr = [].concat(this.data.consume0,this.data.consume1);
        let selectRoom = [].concat(this.data.selectRoom0,this.data.selectRoom1);
        let price = 0;
        min_consumptionArr.forEach( val => {
            price += Number(val)
        });
        let data = {"name":this.data.address_name,"phone":this.data.address_tel,"desk_room":desk_room,"arrive_time":this.data.arrive_time,"shop_id":this.data.shop_id,"min_consumption":price,'selectRoom':selectRoom}
        console.log(data);
        if(desk_room.length > 0){
            wx.navigateTo({
                url: '../advList/advList?data='+JSON.stringify(data)
            })
        }else{
            wx.showModal({
                title: '提示',
                content: '请选择房桌',
                showCancel: false
            })
        }
        
    },

    seeRoom(e) {
        let room = e.currentTarget.dataset.room;
        this.setData({
            room: room,
            seeDetail: true
        })
    },
    hiddenDetail() {
        this.setData({
            seeDetail: false
        })
    }

})